# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

source "$(dirname ${BASH_SOURCE[0]})"/../mingw-w64-PKGBUILD-common/kde-frameworks
_realname=kdoctools
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.14.0
pkgrel=1
pkgdesc="Documentation generation from docbook (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: kdoctools'
)
msys2_repository_url='https://invent.kde.org/frameworks/kdoctools/'
url='https://community.kde.org/Frameworks/'
license=('spdx:LGPL-2.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-docbook-xml"
  "${MINGW_PACKAGE_PREFIX}-docbook-xsl"
  "${MINGW_PACKAGE_PREFIX}-karchive"
  "${MINGW_PACKAGE_PREFIX}-libxml2"
  "${MINGW_PACKAGE_PREFIX}-libxslt"
  "${MINGW_PACKAGE_PREFIX}-qt6-base"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-extra-cmake-modules"
  "${MINGW_PACKAGE_PREFIX}-gettext-tools"
  "${MINGW_PACKAGE_PREFIX}-ki18n"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-qt6-tools"
  "perl-URI"
)
groups=("${MINGW_PACKAGE_PREFIX}-kf6")
install="${_realname}-${MSYSTEM}.install"
source=("https://download.kde.org/stable/frameworks/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"{,.sig}
        "0001-kdoctools-relocate-datapath.patch")
sha256sums=('acb9c761e8e10c30f2c32061f64096965459a0513250edf4432f40831a0f536e'
            'SKIP'
            'ec3df1e402a3f1d1adc136f3344c24b772355cbea6869f5d290645ecb12f832e')
validpgpkeys=(
  'E0A3EB202F8E57528E13E72FD7574483BB57B18D' # Jonathan Esk-Riddell <jr@jriddell.org>
  '90A968ACA84537CC27B99EAF2C8DF587A6D4AAC1' # Nicolas Fella <nicolas.fella@kde.org>
)

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-kdoctools-relocate-datapath.patch"
}
build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  _kde_build_env
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_KDE_INSTALL_DIRS[@]}" \
      -DCMAKE_INSTALL_DATAROOTDIR=share \
      -DPERL_EXECUTABLE=/usr/bin/perl \
      -DBUILD_QCH=OFF \
      -DBUILD_TESTING=OFF \
      "${_extra_config[@]}" \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/share/kf6/kdoctools/customization/dtd/kdedbx45.dtd
  for file in "${pkgdir}${MINGW_PREFIX}"/share/kf6/kdoctools/customization/xsl/*.xml; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${file}"
  done
  for file in "${pkgdir}${MINGW_PREFIX}"/share/kf6/kdoctools/customization/*.xsl; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${file}"
  done
}
